<?php namespace Core\Myapp\Database;

class Querybuider
{
    protected $_model;

    protected $_query = '';

    protected $_paramsToExecute = array();

    protected function getModel()
    {
        return $this->_model;
    }

    public function setModel($model)
    {
        $this->_model = $model;
        return $this;
    }

    protected function setParams($params)
    {
        $this->_paramsToExecute = $params;
        return $this;
    }

    public function getParamsToExecute()
    {
        return $this->_paramsToExecute;
    }

    protected function buidSelectSQL()
    {
        $query = '';
        $query .= 'SELECT ' . $this->getSelect() . ' ';
        $query .= 'FROM ' . $this->getTable() .' ';
        if($this->getJoins()) {
            foreach($this->getJoins() as $join) {
                $query .= strtoupper($join['type']) . ' ' . $join['table'] . ' ON ';
                $query .= $this->convertJoinOnCondition($join) . ' ';
            }
        }
        if($this->getConditions())
            $query .= 'WHERE ' . $this->getConditions() .'';
        $params[":id"] = $this->getModel()->getId();
        $this->setParams($params);
        return $query;
    }

    protected function buidUpdateSQL()
    {
        $data = $this->getModel()->getData();
        unset($data[$this->getModel()->getPrimaryId()]);
        $query = '';
        $query .= 'UPDATE ' . $this->getTable() . ' ';
        $query .= '(' . implode(',', array_keys($this->getdata())) .') ';
        $query .= 'VALUES (:' . implode(',:', array_keys($this->getdata())) . ':) ';
        $query .= 'WHERE ' . $this->getConditions() .' ';
        // Buid params
        $params = array();
        foreach($data as $key=>$value) {
            $params[":{$key}"] = $value;
        }
        $params[":id"] = $this->getModel()->getId();
        $this->setParams($params);
        return $query;
    }

    protected function buidInsertSQL()
    {
        $data = $this->getModel()->getData();
        unset($data[$this->getModel()->getPrimaryId()]);
        $query = '';
        $query .= 'INSERT INTO ' . $this->getTable() . ' ';
        $query .= ' (' . implode(',', array_keys($data)) .') ';
        $query .= ' VALUES  (:' . implode(',:', array_keys($data)) . ')';
        // Buid params
        $params = array();
        foreach($data as $key=>$value) {
            $params[":{$key}"] = $value;
        }
        $this->setParams($params);
        return $query;
    }

    protected function getSelect()
    {
        $select = implode(',', $this->getModel()->getSelect());
        return $select;
    }

    protected function getTable()
    {
        return $this->getModel()->getTable();
    }

    protected function getConditions()
    {
        return implode(' AND ', $this->getModel()->getConditions());
    }

    protected function getJoins()
    {
        return $this->getModel()->getJoins();
    }

    protected function convertJoinOnCondition($join)
    {
        $on = str_replace('mt', $this->getTable(), $join['on']);
        $on = str_replace('jt', $join['table'], $on);
        return $on;
    }

    protected function setQuery()
    {
        if($this->getModel()->getQueryType()=='UPDATE')
            $this->_query = $this->buidUpdateSQL();
        elseif ($this->getModel()->getQueryType()=='SELECT')
            $this->_query = $this->buidSelectSQL();
        elseif ($this->getModel()->getQueryType()=='INSERT')
            $this->_query = $this->buidInsertSQL();
        return $this;
    }

    public function getQuery()
    {
        if(!$this->_query)
            $this->setQuery();
        return $this->_query;
    }
}